package com.spring.courseapi.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spera.genutil.TokenUtil;
import com.spring.comment.web.service.CommentService;
import com.spring.courseapi.web.dao.CourseApiMapper;
import com.spring.courseapi.web.model.Course;
import com.spring.courseapi.web.model.Course2;
import com.spring.courseapi.web.model.CourseAppointment;
import com.spring.courseapi.web.model.CourseOpen;
import com.spring.courseapi.web.model.CourseOpenDetail;
import com.spring.courseapi.web.model.CourseType;
import com.spring.courseapi.web.model.MyCourseOpenDetail;
import com.spring.courseapi.web.model.Teacher;
import com.spring.courseapi.web.service.CourseApiService;
import com.spring.iosservice.web.model.BabyCourseVideo;
import com.spring.iosservice.web.service.IosQuestService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;

@Service
public class CourseApiServiceImpl implements CourseApiService{
	@Resource
	private CourseApiMapper CourseApiMapper;
	
	@Resource
	private CommentService commentService;
	
	@Resource
	private IosQuestService iosQuestService;
	
	@Resource
	private JedisCache jedisCache;
	
	
	//课程科目类型集合 
	@Override
	public Map<String, Object> getCourseTypeList() throws Exception {
		Map<String, Object> m = new HashMap();
		
		/*Map<String, Object> t = checkToken(token);
		if (t != null) {

			return t;

		}

		String c_user_id = redisCache.lindex(token, 0);*/
		
		
			/*
			HashMap hmc=MyTools.checkIsNotNull(rechargecard_id);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			
			
			HashMap hm=new HashMap();
			hm.put("card_id", rechargecard_id);
			hm.put("c_family_member_id", c_family_member_id); SpUtil*/
			
			List<CourseType> list = CourseApiMapper.getCourseTypeList();
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
	
	
	
	
	
//课程查询
	@Override
	public Map<String, Object> getCourseList(String course_type_id,String course_name,String course_user_type_code,String course_base_id) throws Exception {
		Map<String, Object> m = new HashMap();
	

			HashMap hm=new HashMap();
			hm.put("course_type_id", course_type_id);
			hm.put("course_name", course_name); 
			hm.put("course_user_type_code", course_user_type_code); 
			hm.put("course_base_id", course_base_id); 
			
			List<Course> list = CourseApiMapper.getCourseList(hm);
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
	


//开课信息查询
	@Override
	public Map<String, Object> getCourseOpenList(String course_id,String course_open_detail_id,String class_name,String course_open_id,
			String kids_park_id,String page_size,String page_index,String skip,String rows) throws Exception {
		
		Map<String, Object> m = new HashMap();
		
		/*Map<String, Object> t = checkToken(token);
		if (t != null) {

			return t;

		}

		String c_user_id = redisCache.lindex(token, 0);*/
		
		
			
			/*HashMap hmc=MyTools.checkIsNotNull(course_type_id,course_name,course_user_type_code,course_id);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			*/
			
			
			HashMap hm=MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
			hm.put("course_id", course_id);
			hm.put("class_name", class_name); 
			hm.put("course_open_id", course_open_id); 
			hm.put("kids_park_id", kids_park_id); 
			
			List<CourseOpen> list = CourseApiMapper.getCourseOpenList(hm);
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
	
	

	
	//课程表查询
	@Override
	public Map<String, Object> getCourseOpenDetailList(String course_id,String course_base_id,String  course_open_detail_id,String start_time,String end_time,
			String kids_park_id,String page_size,String page_index,String skip,String rows) throws Exception {
		
		Map<String, Object> m = new HashMap();
		
		/*Map<String, Object> t = checkToken(token);
		if (t != null) {

			return t;

		}

		String c_user_id = redisCache.lindex(token, 0);*/
		
	
			
			HashMap hmc=MyTools.checkIsNotNull(kids_park_id);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			
			
			
			HashMap hm=MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
			hm.put("course_id", course_id);
			hm.put("course_base_id", course_base_id);
			hm.put("course_open_detail_id", course_open_detail_id);
			hm.put("start_time", start_time); 
			hm.put("end_time", end_time); 
			hm.put("kids_park_id", kids_park_id); 
			
			System.out.println(hm.toString());
			
			List<CourseOpenDetail> list = CourseApiMapper.getCourseOpenDetailList(hm);
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}

	
	@Override
	public Map<String, Object> getMyCourseOpenDetailList(String token,String course_id,String c_user_id,String course_base_id,String  course_open_detail_id,String start_time,String end_time,
			String kids_park_id,String page_size,String page_index,String skip,String rows) throws Exception {
		
	   	Map<String, Object> m = new HashMap();
	   	
	   	Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
	   	if (t != null) {
	   		return t;
	   	}
	   		
	   	//	String c_user_id = jedisCache.lindex(token, 0);
	   	
	   		HashMap hmc=MyTools.checkIsNotNull(kids_park_id,token);
	   		if(ApplicationUtils.isNotNull(hmc)){
	   			return hmc;
	   		}
			
			HashMap hm=MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
			hm.put("course_id", course_id);
			hm.put("course_base_id", course_base_id);
			hm.put("course_open_detail_id", course_open_detail_id);
			hm.put("start_time", start_time); 
			hm.put("end_time", end_time); 
			hm.put("kids_park_id", kids_park_id); 
			
			hm.put("c_user_id", c_user_id); 
			
			System.out.println(hm.toString());
			
			List<MyCourseOpenDetail> list = CourseApiMapper.getMyCourseOpenDetailList(hm);
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
	
	
	

	//预约课程
	@Override
	public Map<String, Object> appointCourse(String token,String course_open_detail_id,String c_user_id,
			String course_user_type_code,String real_name,String phone) throws Exception {
		
		Map<String, Object> m = new HashMap();
		
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);  
		if (t != null) {

			return t;

		}

//		String myc_user_id = jedisCache.lindex(token, 0);
		
			
			
			HashMap hmc=MyTools.checkIsNotNull(course_open_detail_id,c_user_id,course_user_type_code,real_name);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			
			
			
			HashMap hm=new HashMap();
			hm.put("course_open_detail_id", course_open_detail_id);
			hm.put("c_user_id", c_user_id); 
			hm.put("course_user_type_code", course_user_type_code); 
			hm.put("real_name", real_name); 
			hm.put("phone", phone); 
			
			//验证课程id
			int count = CourseApiMapper.appointCourseCheckCourseId(hm);
			
			if(count!=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效的课程id");
				return m;
			}
			
			
			//验证是否重复预约
			int count2 = CourseApiMapper.appointCourseCheckAppointment(hm);
			
			if(count2>=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "无法重复预约");
				return m;
			}
			
			if(phone!=null){
			int count3 = CourseApiMapper.appointCourseCheckPhone(hm);
			if(count3>=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "无法重复预约");
				return m;
			}}
			
			//插入申请记录
			int effect=CourseApiMapper.appointCourse(hm);
			if(effect!=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "预约失败");
				return m;
			}
			
			
			
			
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "插入成功");

		
		return m;
	}

	
	//获取用户预约的课程接口/查看签到记录
		@Override
		public Map<String, Object> getCourseAppointmentList(String token,String c_user_id,
				String course_user_type_code,String isattended,String start_time,String end_time
				,String page_size,String page_index,String skip,String rows) throws Exception {
			
			Map<String, Object> m = new HashMap();
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);  
			if (t != null) {

				return t;

			}

//			String myc_user_id = jedisCache.lindex(token, 0);
			
			try {
				
				HashMap hmc=MyTools.checkIsNotNull(c_user_id,course_user_type_code);
				if(ApplicationUtils.isNotNull(hmc)){
					return hmc;
				}
				
				HashMap hm=MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
				hm.put("c_user_id", c_user_id);
				hm.put("course_user_type_code", course_user_type_code); 
				hm.put("isattended", isattended); 
				hm.put("start_time", start_time); 
				hm.put("end_time", end_time); 
				hm.put("start_time", start_time); 
				
				
				List<CourseAppointment> list = CourseApiMapper.getCourseAppointmentList(hm);
				
				m.put(ApplicationUtils.DATASET, list);
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "查询成功");

			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();
			}
			return m;
		}
		
		
		
		
	// 取消预约
	@Override
	public Map<String, Object> cancelCourse(String token,
			String course_appointment_id) throws Exception {

		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {

			return t;

		}

//		String c_user_id = jedisCache.lindex(token, 0);

		HashMap hmc = MyTools.checkIsNotNull(course_appointment_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		int count = CourseApiMapper.cancelCourseCheckId(course_appointment_id);
		//检验id
		if (count != 1) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "输入有效course_appointment_id");
		}

		int effect = CourseApiMapper.cancelCourse(course_appointment_id);
		//取消预约
		if (effect == 1) {
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "更新成功");
		} else {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "更新失败");
		}

		return m;
	}
	
	// 家长评论课程接口
		@Override
		public Map<String, Object> parentCommentCourse(String token,
				String course_open_detail_id,String comment,String c_user_name) throws Exception {

			Map<String, Object> m = new HashMap();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {

				return t;

			}

			String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);

			HashMap hmc = MyTools.checkIsNotNull(course_open_detail_id,comment,c_user_name);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}
			
			
			HashMap hm=new HashMap();
			hm.put("c_user_id", c_user_id);
			hm.put("course_open_detail_id", course_open_detail_id);
			hm.put("comment", comment);
			hm.put("c_user_name", c_user_name);
			
			
			int count = CourseApiMapper.parentCommentCourse(hm);
			
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "更新成功");
			

			return m;
		}
		
		//获取课程视频照片辅导员反馈
		public Map<String, Object> getCourseCommentList(String token, String course_open_detailID,String baby_id) throws Exception {
			Map<String, Object> m = new HashMap<String, Object>();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}
			
//			String c_user_id = jedisCache.lindex(token, 0);
			
			HashMap<String, Object> hmc = MyTools.checkIsNotNull(course_open_detailID);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}
			

			
			Map<String, Object> teacher_comment=commentService.getCommentCustomer("1002", course_open_detailID, baby_id, null, "3", null, null, "0","10000");
			
			//Map<String, Object> teacher_comment = commentService.getCommentServiceItem("1001",course_open_detailID , baby_id,"1",null,null,"0","10000");
			

			List<BabyCourseVideo> course_file = iosQuestService.getBabyCourseSpec(null, baby_id, course_open_detailID);
			
			Map<String, Object> dateset=new HashMap<String, Object>();
			
			dateset.put("teacher_comment", teacher_comment.get(ApplicationUtils.DATASET));
			dateset.put("course_file", course_file);
			
			m.put(ApplicationUtils.DATASET, dateset);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "更新成功");
			return m;
		}
		
		
		
		@Override
		public Map<String, Object> getCourseRateList(String token,String course_open_detail_id,
				String pageSize,String pageIndex,String skip,String rows) throws Exception {

			Map<String, Object> m = new HashMap();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}

//			String c_user_id = jedisCache.lindex(token, 0);

			HashMap hmc = MyTools.checkIsNotNull(course_open_detail_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}
			
			Map<String, Object> map=commentService.getCommentItem("1002", course_open_detail_id, null, null, 
					"2", pageSize, pageIndex, skip, rows);
				//	getCommentServiceItem("1001", course_open_detail_id, null, "2",pageSize, pageIndex, skip, rows);		

			return map;
		}
		
		@Override
		public Map<String, Object> getCourseByID(String courseID) throws Exception {

			Map<String, Object> m = new HashMap();
			HashMap<String, Object> hm = new HashMap<String, Object>();

			HashMap hmc = MyTools.checkIsNotNull(courseID);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}
			hm.put("courseID", courseID);
			List<HashMap<String,Object>> list=CourseApiMapper.getCourseByID(hm);

			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");
			return m;
		}
		
		@Override
		public Map<String, Object> getTeacherByID(String t_user_id) throws Exception {

			Map<String, Object> m = new HashMap();
			HashMap<String, Object> hm = new HashMap<String, Object>();

			HashMap hmc = MyTools.checkIsNotNull(t_user_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}
			hm.put("t_user_id", t_user_id);
			List<HashMap<String,Object>> list=CourseApiMapper.getTeacherByID(hm);

			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");
			return m;
		}
		
		@Override
		public Map<String, Object> getToDayCourse(String kids_parkID) throws Exception {

			Map<String, Object> m = new HashMap();
			HashMap<String, Object> hm = new HashMap<String, Object>();

			hm.put("kids_parkID", kids_parkID);
			
			List<HashMap<String,Object>> list=CourseApiMapper.getToDayCourse(hm);

			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");
			return m;
		}
}
